package com.kanshu.ksgb.fastread.doudou.common.business.ad.toutiao;

import android.support.annotation.StringRes;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;

import java.text.DecimalFormat;

/**
 * Created by yanfuchang on 2017/11/20.
 */

public class StringUtils {
    private static final long K = 1024;
    private static final long M = K * K;
    private static final long G = M * K;
    private static final long T = G * K;

    public static String bytesToHuman(final long value) {
        final long[] dividers = new long[]{T, G, M, K, 1};
        final String[] units = new String[]{"TB", "GB", "MB", "KB", "B"};
        if (value < 1)
            return 0 + " " + units[units.length - 1];
        String result = null;
        for (int i = 0; i < dividers.length; i++) {
            final long divider = dividers[i];
            if (value >= divider) {
                result = format(value, divider, units[i]);
                break;
            }
        }
        return result;
    }

    private static String format(final long value,
                                 final long divider,
                                 final String unit) {
        final double result =
                divider > 1 ? (double) value / (double) divider : (double) value;
        return new DecimalFormat("#.##").format(result) + " " + unit;
    }


    public static String getString(@StringRes int id) {
        return Xutils.getContext().getResources().getString(id);
    }

    public static String getString(@StringRes int id, Object... formatArgs) {
        return Xutils.getContext().getResources().getString(id, formatArgs);
    }

    /**
     * 计算两个时间戳差值
     *
     * @return 返回差值（分钟）
     */
    public static long getDistanceTime(long time1, long time2) {
        long diff;
        long minute;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        minute = diff / (60 * 1000);
        return minute;
    }
}
